'use strict';

const Service = require('egg').Service;

class UserService extends Service {
    // resources  index / create / destroy / update

    // 查询
    async index() {
        try {
            let params = this.ctx.request.query;
            // 超级管理员 有权限访问用户数据
            if (params.username !== "admin") return { code: 400, data: "获取失败" }
            let pageNum = Number(params.currentPage) || 1;
            let pageSize = Number(params.pagesize) || 10;
            let res = await this.app.model.User.findAndCountAll({
                offset: pageNum * pageSize - pageSize,
                limit: pageSize
            });
            // 返回密码 极不友好 遍历数组将密码设置为 undefind
            res.rows.forEach(item => {
                item.password = undefined
            });
            return res
        } catch (err) { console.log(err) }
    }
    // 查询
    async indexUser() {
        try {
            let username = this.ctx.request.query.username;
            let res = await this.app.model.User.findAll({
                where: {
                    username
                }
            });
            return res
        } catch (err) { console.log(err) }
    }
    // 添加
    async create() {
        try {
            // let params = this.ctx.request.body.params;
            let username = this.ctx.request.body.username;
            let password = this.ctx.request.body.password;
            let res = await this.app.model.User.findAll({ where: { username } }) || [];
            if (res.length == 0) {
                this.app.model.User.create({
                    username,
                    password,
                    showorhide: false
                });
                return {
                    code: 200,
                    msg: "添加成功"
                }
            } else {
                return {
                    code: 400,
                    msg: "添加失败"
                }
            }
        } catch (err) { console.log(err) }
    }
    // 删除
    async destroy() {
        try {
            let id = this.ctx.params.id;
            await this.app.model.User.destroy({
                where: {
                    id
                }
            })
            let res = await this.app.model.User.findAll();
            return res
        } catch (err) { console.log(err) }
    }
    // 修改个人资料
    async update() {
        try {
            let userPersonal = this.ctx.request.body.params;
            await this.app.model.User.update({
                userimg: userPersonal.userimg,
                nickname: userPersonal.nickname,
                mail: userPersonal.mail,
                phone: userPersonal.phone,
                sex: userPersonal.sex
            }, {
                where: {
                    username: userPersonal.username
                }
            })
            let res = await this.app.model.User.findAll({
                where: {
                    username: userPersonal.username
                }
            })
            // console.log(res)
            return res
        } catch (err) { console.log(err) }
    }
}

module.exports = UserService;
